<template>
  <div>
    <el-upload
      list-type="picture-card"
      :limit="1"
      action="#"
      :on-preview="preview"
      :on-remove="remove"
      :on-success="success"
      :before-upload="beforeUpload"
      :http-request="upload"
      :file-list="fileList"
      :class="{disabled : fileComponent}"
    >
      <i class="el-icon-plus" />
    </el-upload>
    <el-progress v-if="showPerson" :percentage="percent" color="blue" style="width:45%" />
    <el-dialog :visible.sync="showDialog" style="width:100%" title="预览图片">
      <img :src="imageUrl" alt="">
    </el-dialog>
  </div>
</template>

<script>
import Cos from 'cos-js-sdk-v5'
var cos = new Cos({
  SecretId: 'AKID5ybz7yZu1rRnwnNhkX3Iivtb5KIzPZS4',
  SecretKey: 'MSHp67rKWRgeLlvoQlxtYw3uVpsfBPM2'
})
export default {
  data() {
    return {
      fileList: [],
      imageUrl: '',
      showDialog: false,
      checkFileid: null,
      percent: 0,
      showPerson: false
    }
  },
  computed: {
    fileComponent() {
      return this.fileList.length === 1
    } },
  methods: {
    //   预览
    preview(file) {
      this.imageUrl = file.url
      this.showDialog = true
    },
    // 删除
    remove(file, fileList) {
      this.fileList = fileList
    },
    // 上传
    success(file, fileList) {
      this.fileList = fileList.map(item => item)
    },
    // 上传之前的处理
    beforeUpload(file) {
      console.log('files', file)
      const types = ['image/jpeg', 'image/gif', 'image/bmp', 'image/png', 'image/jpg']
      if (!types.some(item => item === file.type)) {
        //   不成功
        this.$message.error('图片格式不正确')
        return false
      }

      const maxSize = 5 * 1024 * 1024
      if (!file.size > maxSize) {
        //   不成功
        this.$message.error('图片过大')
        return false
      }
      this.showPerson = true
      this.checkFileid = file.uid
      this.fileList.push(file)

      return true
    },
    // 上传图片
    upload(params) {
      console.log('params', params)
      if (params.file) {
        cos.putObject({
          Bucket: 'tangzixin-007-1309086070', // 存储桶
          Region: 'ap-beijing', // 地域
          Key: params.file.name, // 文件名
          Body: params.file, // 上传的文件对象
          StorageClass: 'STANDARD', // 上传的模式类型
          onProgress: params => {
            console.log(params)
            this.percent = params.percent * 100
          }
        }, (err, data) => {
          // 判断无论成功或者失败都能打印
          console.log('ss', err || data)
          //   判断statusCode为200的时候就表示上传成功
          // 再查询该图片是否与数组的图片有没有相同的，没有就将地址添加到数组中
          if (!err && data.statusCode === 200) {
            this.fileList = this.fileList.map(item => {
              if (item.uid === this.checkFileid) {
                return { url: 'http://' + data.Location, upload: true }
              }
              return item
            })
          }
        })
        setTimeout(() => {
          this.showPerson = false
          this.percent = 0
        }, 2000)
      }
    }
  }
}
</script>

<style>
.disabled .el-upload--picture-card {
  display: none
}
</style>
